package com.shujia;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Solution {


    @Test
    public void exceptionDemo() {
        try {


            System.out.println(111);
            System.out.println(1 / 0);
            return;
        } catch (Exception e) {
            e.printStackTrace();
            return;
        } finally {
            System.out.println("finally....");
        }
    }

    @Test
    public void test122() {
        int[] arr = {3, 2, 4};
        int target = 6;
        int[] ints = twoSum(arr, target);
        System.out.println(Arrays.toString(ints));
    }

    public int[] twoSum(int[] nums, int target) {
        int[] arr = new int[2];
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            // 判断, 如果target 减去 当前遍历的数字的值在map里, 直接获取map中的下标和当前遍历的下标
            if (map.containsKey(target - nums[i])) {
                int firstIndex = map.get(target - nums[i]);

                arr[0] = firstIndex;
                arr[1] = i;
                return arr;
            }
            // map里面保存数组的数字和下标
            map.put(nums[i], i);

        }

        return arr;

    }
}
